package com.tentative.competition.schedule.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tentative.competition.common.core.result.PageResult;
import com.tentative.competition.common.model.schedule.param.ScheduleNewsInfoAddParam;
import com.tentative.competition.common.model.schedule.resp.HomeDynamicStateResp;
import com.tentative.competition.common.model.schedule.resp.ScheduleNewsInfoResp;
import com.tentative.competition.common.model.schedule.vo.FileVo;
import com.tentative.competition.schedule.domain.ScheduleNewsInfo;
import com.tentative.competition.schedule.mapper.ScheduleNewsInfoMapper;
import com.tentative.competition.schedule.service.ScheduleFileInfoService;
import com.tentative.competition.schedule.service.ScheduleNewsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 大赛新闻信息 服务实现类
 * </p>
 *
 * @author SmileH
 * @since 2023-06-22
 */
@Service
public class ScheduleNewsInfoServiceImpl extends ServiceImpl<ScheduleNewsInfoMapper, ScheduleNewsInfo> implements IService<ScheduleNewsInfo> , ScheduleNewsInfoService {

    @Lazy
    @Autowired
    private ScheduleFileInfoService fileInfoService;

    @Override
    public PageResult<ScheduleNewsInfoResp> pageList(ScheduleNewsInfoAddParam param) {
        IPage<ScheduleNewsInfoResp> infoResps = baseMapper.pageList(param,new Page<>(param.getPageNum(),param.getPageSize()));
        if (CollUtil.isEmpty(infoResps.getRecords())){
            return new PageResult<>();
        }
        //组装附件信息
        Set<Long> logoFileIds = infoResps.getRecords().stream().map(ScheduleNewsInfoResp::getNewsPropagate).collect(Collectors.toSet());
        if (CollUtil.isNotEmpty(logoFileIds)){
            Map<Long, FileVo> longFileVoMap = fileInfoService.selectFileMapByIds(logoFileIds);
            infoResps.getRecords().forEach(x->x.setNewsPropagateVo(longFileVoMap.get(x.getNewsPropagate())));
        }
        PageResult<ScheduleNewsInfoResp> pageResult = new PageResult<>();
        pageResult.setContent(infoResps.getRecords());
        pageResult.setTotal(infoResps.getTotal());
        return pageResult;
    }

    @Override
    public ScheduleNewsInfoResp getInfo(Long id) {
        ScheduleNewsInfoResp resp = new ScheduleNewsInfoResp();
        ScheduleNewsInfo newsInfo = this.lambdaQuery()
                .eq(ScheduleNewsInfo::getId, id)
                .list()
                .stream()
                .findFirst()
                .orElse(null);
        if (ObjUtil.isNotEmpty(newsInfo)){
            BeanUtil.copyProperties(newsInfo,resp);
            //组装附件
            if (ObjUtil.isNotEmpty(resp.getNewsPropagate())){
                Map<Long, FileVo> longFileVoMap = fileInfoService.selectFileMapByIds(CollUtil.newHashSet(resp.getNewsPropagate()));
                resp.setNewsPropagateVo(longFileVoMap.get(resp.getNewsPropagate()));
            }
        }
        return resp;
    }

    @Override
    public void addOrEdit(ScheduleNewsInfoAddParam param) {
        ScheduleNewsInfo info = new ScheduleNewsInfo();
        BeanUtil.copyProperties(param,info);
        this.saveOrUpdate(info);
    }

    @Override
    public Boolean deleteOrgInfo(List<Long> ids) {
        if (CollUtil.isEmpty(ids)){
            throw new RuntimeException("没有需要删除的数据！");
        }
        return this.lambdaUpdate()
                .in(ScheduleNewsInfo::getId,ids)
                .set(ScheduleNewsInfo::getDelFlag,1)
                .update();
    }

    @Override
    public List<HomeDynamicStateResp> getDynamicInfo(Long scheduleId,Boolean isAll) {
        List<ScheduleNewsInfo> newsInfos = CollUtil.newArrayList();
        if (BooleanUtil.isTrue(isAll)){
            List<ScheduleNewsInfo> list = this.lambdaQuery()
                    .eq(ScheduleNewsInfo::getScheduleId, scheduleId)
                    .eq(ScheduleNewsInfo::getDelFlag, 0)
                    .list();
            newsInfos.addAll(list);
        }else {
            List<ScheduleNewsInfo> list = this.lambdaQuery()
                    .eq(ScheduleNewsInfo::getScheduleId, scheduleId)
                    .eq(ScheduleNewsInfo::getDelFlag, 0)
                    .eq(ScheduleNewsInfo::getIsShow, 1)
                    .list();
            newsInfos.addAll(list);
        }
        if (CollUtil.isEmpty(newsInfos)){
            return CollUtil.newArrayList();
        }
        List<HomeDynamicStateResp> respList = CollUtil.newArrayList();
        List<HomeDynamicStateResp> dynamicStateResps = newsInfos.stream().map(x -> {
            HomeDynamicStateResp resp = new HomeDynamicStateResp();
            String content = x.getNewsContent();
            String disContent = content.replaceAll("\\&[a-zA-Z]{0,9};", "").replaceAll("<[^>]*>", "");
            resp.setNewsId(x.getId());
            resp.setContent(x.getNewsContent());
            resp.setFileId(x.getNewsPropagate());
            resp.setPublishTime(x.getCreateTime());
            resp.setTitle(x.getNewsTitle());
            resp.setDisContent(disContent.length() > 50 ? disContent.substring(0,49) + "..." : disContent);
            return resp;
        }).collect(Collectors.toList());
        respList.addAll(dynamicStateResps);
        //组装附件信息
        Set<Long> logoFileIds = respList.stream().map(HomeDynamicStateResp::getFileId).collect(Collectors.toSet());
        if (CollUtil.isNotEmpty(logoFileIds)){
            Map<Long, FileVo> longFileVoMap = fileInfoService.selectFileMapByIds(logoFileIds);
            respList.forEach(x->x.setImgUrl(longFileVoMap.get(x.getFileId()).getFileUrl()));
        }
        return respList;
    }
}
